package com.gdth.bank.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;

import com.gdth.bank.BankApplication;
import com.gdth.bank.model.TAppCanshuModel;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.TXtBm;
import com.gdth.model.Item;
import com.gdth.utils.BaseAppUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import org.greenrobot.greendao.AbstractDao;


/** 
 * @author  Arvin: 
 * @version 2016年5月27日 下午4:06:18 
 * 
 */
public class AppUtil extends BaseAppUtil{
	
	public static final String setting = "setting_shared";

	public static final String DB_NAME = "daoexample_db";
//	public static final String DB_NAME = "gdthdb.db";
	public static final String SHARE_Application = "Application";
	public static final String SHARE_LOGIN_STATE = "SHARE_LOGIN_STATE";

	public static final String SHARE_LOGIN_ClientPublicKey = "SHARE_LOGIN_ClientPublicKey";
	public static final String SHARE_LOGIN_ClientPrivateKey = "SHARE_LOGIN_ClientPrivateKey";
	public static final String SHARE_LOGIN_ServerPublicKey = "SHARE_LOGIN_ServerPublicKey";
	
	/** 用来操作SharePreferences的标识 */
	public static final String SHARE_LOGIN_TAG = SHARE_Application;
    
	/** username */
	public static final String SHARE_LOGIN_USERNAME = "MAP_LOGIN_USERNAME";
    /** password */
	public static final String SHARE_LOGIN_PASSWORD = "MAP_LOGIN_PASSWORD";
	/** sfz */
	public static final String SHARE_LOGIN_SFZ = "MAP_LOGIN_SFZ";
	public static final String SHARE_DATA_USER = "DATA_USER";
	public static final String SHARE_DATA_BM = "DATA_BM";
	public static final String SHARE_DATA_TOKEN = "SHARE_DATA_TOKEN";
	
	/** zjlx **/
	public static final String SHARE_DATA_ZJLX_ZDYX = "ZJ_LX_ZDYX";
	/** zjlx **/
	public static final String SHARE_DATA_ZJLX_DZCP = "ZJ_LX_DZCP";
	/** zjlx **/
	public static final String SHARE_DATA_ZJLX_CJKH = "ZJ_LX_CJKH";
	/** zczl **/
	public static final String SHARE_DATA_ZCZL = "ZC_ZL";
	/** jtgx **/
	public static final String SHARE_DATA_JTGX = "JT_GX";
	//  时间: 2020/5/9 8:25 AM ,作者: xjc, 作用: 成员职业
	public static final String SHARE_DATA_CYZY = "CY_ZY";
	//  时间: 2020/5/12 6:01 PM ,作者: xjc, 作用:资产名称
	public static final String SHARE_DATA_ZCMC = "ZC_MC";
	/** yxzl **/
	public static final String SHARE_DATA_YXZL = "YX_ZL";
	
	public static final String SHARE_DATA_BFLX = "BF_LX";
	
	public static final String SHARE_DATA_CPLX = "CP_LX";
	
	public static final String SHARE_DATA_FJLX = "FJ_LX";
	
	public static final String SHARE_DATA_KHLB = "KH_LB";
	
	public static final String SHARE_DATA_ZDYX = "ZDYX_BZ";
	
	public static final String SHARE_DATA_SBZT ="SB_ZT";
	public static final String SHARE_DATA_GW ="GW";
	public static final String SHARE_DATA_JJZL ="JJZL";
	
	public static final String SHARE_DATA_Device = "SHARE_DATA_DeviceId";
	
	public static boolean isDegbbuer = true;
	
	public static final int activity_map_requestcode = 101;
	public static final int activity_map_resultcode = 102;
	public static final int activity_txl_requestcode = 201;
	public static final int activity_txl_resultcode = 202;
	public static final int activity_cjjl_requestcode = 301;
	public static final int activity_cjjl_resultcode = 302;
	public static final int activity_photo_requestcode = 401;
	public static final int activity_photo_resultcode = 402;
	public static final int activity_picture_requestcode = 501;
	public static final int activity_picture_resultcode = 502;
	public static final int activity_pic_requestcode = 601;
	public static final int activity_pic_resultcode = 602;
	public static final int activity_phone_requestcode = 701;
	
	public static final int activity_canshu_all = 1001;
	public static final int activity_canshu_yxzl = 1002;
	public static final int activity_canshu_zjlx = 1003;
	public static final int activity_canshu_jtgx = 1004;
	
	public static final int sjzt_cancel = 0;
	public static final int sjzt_delete = 0;
	public static final int sjzt_create = 1;
	public static final int sjzt_update = 2;
	public static final int sjzt_normal = 3;
	
	public static final int jtgx_huzhu = 7;
	
	public static final String NOTIFICATION = "notification";
	public static final String LAST_NOTIFICATION_ID= "lastNotificationid";
	
	
	public static AbstractDao<?, Long> getBaseServer(Class<?> className){
		String name = className.getName();
		name = "get" + name.substring(name.lastIndexOf(".") + 1) + "Dao";
		
		DaoSession mDaoSession = BankApplication.getDaoSession();
		
        Method method = null;
        AbstractDao<?, Long> dao = null;
		try {
//			method = mDaoSession.getClass().getMethod(name, null);
//			Object obj = method.invoke(mDaoSession, null);
			method = mDaoSession.getClass().getMethod(name, new Class[0]);
			Object obj = method.invoke(mDaoSession, new Object[]{});
			dao = (AbstractDao<?, Long>) obj;
		} catch (NoSuchMethodException e){
			// TODO Auto-generated catch block
						e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		return dao;
	}
	
	public static int getColor(Context mContext, int color){
		return mContext.getResources().getColor(color);
//		return ContextCompat.(mContext, color);
	}
	
	
	/**
	 * 获取证件类型
	 * @param mContext
	 * @return
	 */
	public static String getCanshuForJson(Context mContext, String type){
		String json = SharedPreferencesUtil.getSharePreferencesByKey(mContext, AppUtil.SHARE_Application, type);
		
		return json;
	}
	
	/**
	 * 获取证件类型
	 * @param mContext
	 * @return
	 */
	public static List<Item> getCanshuByType(Context mContext, String type){
		String json = SharedPreferencesUtil.getSharePreferencesByKey(mContext, AppUtil.SHARE_Application, type);
		
		return TAppCanshuModel.getCanshuList(json);
	}
	
	public static String getAllBmJson(Context mContext) {
		String json = SharedPreferencesUtil.getSharePreferencesByKey(mContext, AppUtil.SHARE_Application, AppUtil.SHARE_DATA_BM);
		
		return json;
	}
	public static String getAllkhlbJson(Context mContext, String type) {
		String json = SharedPreferencesUtil.getSharePreferencesByKey(mContext, AppUtil.SHARE_Application, AppUtil.SHARE_DATA_KHLB);
		
		return json;
	}
	public static String getAllzdyxJson(Context mContext, String type) {
		String json = SharedPreferencesUtil.getSharePreferencesByKey(mContext, AppUtil.SHARE_Application, AppUtil.SHARE_DATA_ZDYX);
		
		return json;
	}
	
	public static List<TXtBm> getAllBm(Context mContext) {
		String json = getAllBmJson(mContext);
		
		List<TXtBm> mList = new ArrayList<TXtBm>();
		Gson mGson = new Gson();
		
		JsonParser parser = new JsonParser();

		//通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
		JsonElement el = parser.parse(json);

		//把JsonElement对象转换成JsonArray
		JsonArray jsonArray = null;
		if(el.isJsonArray()){
			jsonArray = el.getAsJsonArray();
		} else {
			return mList;
		}

		//遍历JsonArray对象
		Iterator<?> it = jsonArray.iterator();
		while(it.hasNext()){
			JsonElement e = (JsonElement)it.next();
			//JsonElement转换为JavaBean对象
			TXtBm bm = mGson.fromJson(e, TXtBm.class);
			
			mList.add(bm);
		}
		
		return mList;
	}
}
